package org.base.core.utils.http;


import java.nio.charset.Charset;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;


public class HttpUtils {
	
	public static String doGet(String url, Map<String,String> data) {
		try {
			URIBuilder uriBuilder = new URIBuilder(url);
			if(data!=null && !data.isEmpty()) {
				List<NameValuePair> pairs = new ArrayList<>(data.size());
				data.entrySet().forEach((d)->{
					pairs.add(new BasicNameValuePair(d.getKey(), d.getValue()));
				});
				uriBuilder.setParameters(pairs);
			}
			CloseableHttpClient httpClient = createSSLClientDefault();
	    	HttpGet get = new HttpGet(uriBuilder.build());
	    	// 构造消息头
            get.setHeaders(getHeaders());
            //发送请求
            HttpResponse response = httpClient.execute(get);
            return EntityUtils.toString(response.getEntity());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String doPost(String url, String data) {
		
        try {
            CloseableHttpClient httpClient = createSSLClientDefault();
        	HttpPost post = new HttpPost(url);
            // 构造消息头
            post.setHeaders(getHeaders());
            // 构建消息实体
            StringEntity entity = new StringEntity(data, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            post.setEntity(entity);
            //发送请求
            HttpResponse response = httpClient.execute(post);
            return EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	private static Header[] getHeaders() {
		Header[] headers = new Header[2];
		headers[0] = new BasicHeader("Content-type", "application/json; charset=utf-8");
		headers[1] = new BasicHeader("Connection", "Close");
		return headers;
	}
	
	private static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
	}
	
	public static void main(String[] args) {
	}
	
}
